package com.icangya.DAO.impl;

import com.alibaba.fastjson.JSONArray;
import com.icangya.DAO.ICarouselFigureDAO;
import com.icangya.domain.CarouselFigure;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CarouselFigureDAOImpl  extends BaseDAO<CarouselFigure> implements ICarouselFigureDAO{

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public List<CarouselFigure> getAllCarouselFigure() {

        String  hql="FROM CarouselFigure cf ";
        List<CarouselFigure> list= (List<CarouselFigure>) getHibernateTemplate().find(hql);

        if(list.size()>0)
            return list;
        return null;
    }

    @Override
    public int deleteByCarId(Long cid) {

       int result= getHibernateTemplate().execute(new HibernateCallback<Integer>() {

            @Override
            public Integer doInHibernate(Session session) throws HibernateException {

                String hql="delete CarouselFigure cf where cf.id=? ";
                 Query query= session.createQuery(hql);
                 query.setParameter(0,cid);
              int ret=   query.executeUpdate();
                logger.warn("删除的轮播图 == " + cid+"|结果 == " + ret);
//

                return ret;
            }
        });
        return result;
    }


}
